package com.example.neusoft.yuejian.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.yuejian.Activity.ActDetailActivity;
import com.example.neusoft.yuejian.Activity.ClubActivity;
import com.example.neusoft.yuejian.Base.BaseFragment;

import com.example.neusoft.yuejian.Base.BaseListener;
import com.example.neusoft.yuejian.Base.BaseListenner;
import com.example.neusoft.yuejian.Base.BaseModel;
import com.example.neusoft.yuejian.Bean.ClubBean;

import com.example.neusoft.yuejian.Bean.ClubDetailBean;
import com.example.neusoft.yuejian.R;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.List;

import retrofit2.Call;

public class ClubFragment extends BaseFragment implements View.OnClickListener {

    private List<ClubBean> lists;
    private RecyclerView jlb;


    @Override
    public int getLayoutFile() {
        return R.layout.fragment_julebu;
    }

    @Override
    public void initView() {
        jlb = view.findViewById(R.id.jlb);
        jlb.setLayoutManager(new LinearLayoutManager(this.getActivity()));

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        getClub();

    }


    private void getClub() {
        BaseModel<ClubBean> model = new BaseModel<>();
        Call<List<ClubBean>> call = model.service.clublist();
        model.callListEnqueue(call, list);
    }



    BaseListener<List<ClubBean>> list=new BaseListener<List<ClubBean>>() {
        @Override
        public void onResponse(List<ClubBean> clubBeans) {
            lists=clubBeans;
              jlb.setAdapter(new ClubAdapter(lists));
        }

        @Override
        public void onFail(String msg) {

        }
    };



    @Override
    public void onClick(View view) {
    }


    public class ClubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<ClubBean> clubBean;
        private ClubBean list;

        public ClubAdapter(List<ClubBean> mDataList) {

            this.clubBean = mDataList;


        }


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getActivity());
            View v = inflater.inflate(R.layout.jlb_item, parent, false);
            return new ClubAdapter.ViewHolder(v);
        }


        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
            list = clubBean.get(position);
            if (null == list)
                return;
            ClubFragment.ClubAdapter.ViewHolder viewHolder = (ClubFragment.ClubAdapter.ViewHolder) holder;
            viewHolder.jlb_mc.setText(list.getCname());
            Picasso.with(getActivity()).load("http://10.0.2.2/YueJian/public/static/img/clubimg/"
                    + list.getCpic()).into(viewHolder.jlb_pic);
            viewHolder.julebu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(getActivity(), ClubActivity.class);
                    intent.putExtra("jid",list.getCid());
                    startActivity(intent);
                }
            });
        }


        @Override
        public int getItemCount() {
            return clubBean.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder {
         private    ImageView jlb_pic;
         private    TextView jlb_mc;
         private    LinearLayout julebu;

            private ViewHolder(View v) {
                super(v);
                jlb_pic = v.findViewById(R.id.jlb_pic);
                jlb_mc = v.findViewById(R.id.jlb_mc);
                julebu=v.findViewById(R.id.julebu);
            }
        }
    }
}